<?php

namespace App\Http\Controllers\Api\V1\Frontend;


use App\Http\Controllers\BaseController;
use App\Models\Video;
use App\Transformers\Frontend\VideoTransformer;

class VideoController extends BaseController
{
    /**
     * @api                         {get} /frontend/video  课程列表
     * @apiVersion                  1.0.0
     * @apiName                     load_video
     * @apiGroup                    Video
     * @apiDescription              课程列表
     * @apiParam (get param)         {String}   category_id 分类搜索(可选)
     * @apiParam (get param)         {Integer}   page 页码(可选)
     * @apiParam (get param)         {String='imaging', 'videoing'} [include] 获取关联信息 imaging:封面[path:文件路径]; videoing:视频[用户未登录为空]
     * @apiSuccess {Number}          status_code    (200) 状态码
     * @apiSuccess {Object}          data                               列表数组
     * @apiSuccess {Integer}         data.id                             ID
     * @apiSuccess {String}          data.title                          标题
     * @apiSuccess {String}          data.introduction                           简介
     * @apiSuccess {Number}          data.price                             单价
     * @apiSuccess {String}          data.details                         详情
     * @apiSuccess {Boolean}         data.state                         状态
     * @apiSuccess {String}          data.created_at                     创建时间
     * @apiSuccess {String}          data.updated_at                     修改时间
     * @apiSuccess {Object[]}       meta  Metadata
     * @apiSuccess {Object}         meta.pagination  分页信息
     * @apiSuccess {Integer}        meta.pagination.total 查询总数
     * @apiSuccess {Integer}        meta.pagination.count 当前页数量
     * @apiSuccess {Integer}        meta.pagination.per_page 每页大小
     * @apiSuccess {Integer}        meta.pagination.current_page 当前页码
     * @apiSuccess {Integer}        meta.pagination.total_pages 总页数
     * @apiSuccess {Object}         meta.pagination.links 相关链接
     * @apiSuccess {String}         meta.pagination.links.next 下一页链接
     * @apiSuccess {String}         meta.pagination.links.previous 上一页链接
     * @return \Dingo\Api\Http\Response
     */
    public function index()
    {
        $category_id = request()->get('category_id', '');
        $title = request()->get('title', '');
        $videos = Video::where(function ($query) use ($category_id, $title) {
            if ($category_id) {
                $query->where('category_id', $category_id);
            }
            if ($title) {
                $query->where('title', 'like', '%' . $title . '%');
            }
        })->where('status', 1)->orderBy('created_at', 'desc')->paginate(15);
        return $this->response->paginator($videos, new VideoTransformer());
    }

    public function show()
    {
        $id = request()->get('id', 0);
        $model = Video::find($id);
        return $this->response->item($model, new VideoTransformer());
    }
}
